﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace SuperFind.Lib.Core
{
    public delegate void OnFileHandler(FileInfo fileInfo);
    public delegate void OnFolderHandler(DirectoryInfo directoryInfo);

    public class Disk
    {

        public event OnFileHandler OnFile;
        public event OnFolderHandler OnFolder;

        public static List<DiskInfo> DiscoverAttachedDisks()
        {
            List<DiskInfo> disks = new List<DiskInfo>();
            foreach (DriveInfo driveinfo in DriveInfo.GetDrives())
            {
                if ((driveinfo.DriveType == DriveType.Fixed || driveinfo.DriveType == DriveType.Network) && driveinfo.IsReady == true)
                {
                    DiskInfo diskInformation = new DiskInfo();
                    diskInformation.RootDirectory = Convert.ToString(driveinfo.RootDirectory);
                    diskInformation.TotalSize = driveinfo.TotalSize;
                    diskInformation.TotalFreeSpace = driveinfo.TotalFreeSpace;
                    diskInformation.Name = driveinfo.Name;
                    diskInformation.VolumeLabel = driveinfo.VolumeLabel;
                    diskInformation.IsReady = driveinfo.IsReady;
                    diskInformation.DriveFormat = driveinfo.DriveFormat;
                    diskInformation.DriveType = Convert.ToString(driveinfo.DriveType);
                    diskInformation.AvailableFreeSpace = driveinfo.AvailableFreeSpace;
                    disks.Add(diskInformation);
                }

            }

            return disks;
        }
        //void DirSearch(string sDir)
        //{
        //    try
        //    {
        //        foreach (string d in Directory.GetDirectories(sDir))
        //        {
        //            foreach (string f in Directory.GetFiles(d, txtFile.Text))
        //            {
        //                lstFilesFound.Items.Add(f);
        //            }
        //            DirSearch(d);
        //        }
        //    }
        //    catch (System.Exception excpt)
        //    {
        //        Console.WriteLine(excpt.Message);
        //    }
        //}
        public void GetSubDirectoryInfo(string directory)
        {
            GetFileInfo(directory);
            DirectoryInfo directoryInfo = new DirectoryInfo(directory);
            foreach (DirectoryInfo dir in directoryInfo.GetDirectories())
            {
                if (dir.Exists)
                {
                    if (OnFolder != null)
                    {
                        OnFolder(directoryInfo);
                    }
                    GetSubDirectoryInfo(dir.FullName);
                }
            }
        }
        // To do: Check File Read Errors
        public void GetFileInfo(string subdirectory)
        {
            string[] DirectoryList = Directory.GetFiles(subdirectory);
            foreach (string parentDirectory in DirectoryList)
            {
                FileInfo fileInfo = new FileInfo(parentDirectory);
                if (fileInfo.Exists)
                {
                    
                    if (OnFile != null)
                    {
                        OnFile(fileInfo);
                    }
                }
            }
        }

    }
}
